package com.zyg.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zyg.shop.entity.ItemCatEntity;
import com.zyg.shop.service.ItemCatService;
import com.zyg.common.utils.PageUtils;
import com.zyg.common.utils.R;



/**
 * 商品类目
 *
 * @author fufeixiang
 * @email 1810782330@qq.com
 * @date 2021-11-26 14:30:51
 */
@RestController
@RequestMapping("shop/itemcat")
public class ItemCatController {
    @Autowired
    private ItemCatService itemCatService;

    /**
     * 列表(分页）
     */
    @GetMapping("/list")
  //  @RequiresPermissions("shop:itemcat:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = itemCatService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{itemCatId}")
   // @RequiresPermissions("shop:itemcat:info")
    public R info(@PathVariable("itemCatId") String id){
        //根据三级id查询出当前商品类目信息
		ItemCatEntity itemCat = itemCatService.getById(id);
        return R.ok().put("itemCat", itemCat);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("shop:itemcat:save")
    public R save(@RequestBody ItemCatEntity itemCat){
		itemCatService.save(itemCat);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    //@RequiresPermissions("shop:itemcat:update")
    public R update(@RequestBody ItemCatEntity itemCat){
		itemCatService.updateById(itemCat);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
   // @RequiresPermissions("shop:itemcat:delete")
    public R delete(@RequestBody String[] ids){
		itemCatService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /*查询一级商品类目*/
    @GetMapping("/findCategory1")
    public R findCategory1(){

        List<ItemCatEntity> category1List = itemCatService.list(new QueryWrapper<ItemCatEntity>().eq("parent_id",0));

        return R.ok().put("list",category1List);
    }

    /*查询二级商品类目*/
    @GetMapping("/findCategory2/{itemCatId}")
    public R findCategory2(@PathVariable String itemCatId){

        //根据父级id查询出二级类目集合
        List<ItemCatEntity> category2List = itemCatService.list(new QueryWrapper<ItemCatEntity>().eq("parent_id", itemCatId));
        return R.ok().put("list",category2List);
    }

    /*查询三级商品类目*/
    @GetMapping("/findCategory3/{itemCatId}")
    public R findCategory3(@PathVariable String itemCatId){

        //根据父级id查询出三级类目集合
        List<ItemCatEntity> category3List = itemCatService.list(new QueryWrapper<ItemCatEntity>().eq("parent_id", itemCatId));
        return R.ok().put("list",category3List);
    }
}
